package com.bw.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.gmall.realtime.common.base.BaseApp;
import com.bw.gmall.realtime.common.bean.TradePaymentBean;
import com.bw.gmall.realtime.common.constant.Constant;
import com.bw.gmall.realtime.common.function.DorisMapFunction;
import com.bw.gmall.realtime.common.util.DateFormatUtil;
import com.bw.gmall.realtime.common.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @Author sw
 * @CreateTime 2024-11-21
 * 12.7 交易域支付成功各窗口汇总表（练习）
 */
public class DwsTradePaymentSucWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsTradePaymentSucWindow().start(10027,4,
                Constant.DWS_TRADE_PAYMENT_SUC_WINDOW,
                Constant.TOPIC_DWD_TRADE_ORDER_PAYMENT_SUCCESS);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        //清洗数据，分组求支付成功独立用户数和新用户数
        SingleOutputStreamOperator<TradePaymentBean> processStream = getProcessStream(dataStreamSource);
        //设置水位线，开窗聚合
        SingleOutputStreamOperator<TradePaymentBean> result = getResult(processStream);
        result.print();
        //写到doris
        result.map(new DorisMapFunction<>())
                .sinkTo(FlinkSinkUtil.getDorisSink("dws_trade_payment_suc_window"));
    }

    private static SingleOutputStreamOperator<TradePaymentBean> getResult(SingleOutputStreamOperator<TradePaymentBean> processStream) {
         return processStream.assignTimestampsAndWatermarks(WatermarkStrategy
                        .<TradePaymentBean>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                        .withTimestampAssigner((event, timestamp) -> event.getTs())
                        .withIdleness(Duration.ofSeconds(120)))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<TradePaymentBean>() {
                            @Override
                            public TradePaymentBean reduce(TradePaymentBean x, TradePaymentBean y) throws Exception {
                                x.setPaymentSucUniqueUserCount(x.getPaymentSucUniqueUserCount() + y.getPaymentSucUniqueUserCount());
                                x.setPaymentSucNewUserCount(x.getPaymentSucNewUserCount() + y.getPaymentSucNewUserCount());
                                return x;
                            }
                        },
                        new ProcessAllWindowFunction<TradePaymentBean, TradePaymentBean, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<TradePaymentBean, TradePaymentBean, TimeWindow>.Context context, Iterable<TradePaymentBean> iterable, Collector<TradePaymentBean> collector) throws Exception {
                                TradePaymentBean tradePaymentBean = iterable.iterator().next();
                                tradePaymentBean.setStt(DateFormatUtil.tsToDateTime(context.window().getStart()));
                                tradePaymentBean.setEdt(DateFormatUtil.tsToDateTime(context.window().getEnd()));
                                tradePaymentBean.setCurDate(DateFormatUtil.tsToDate(System.currentTimeMillis()));
                                collector.collect(tradePaymentBean);
                            }
                        });
    }

    private static SingleOutputStreamOperator<TradePaymentBean> getProcessStream(DataStreamSource<String> dataStreamSource) {
         return dataStreamSource.map(JSON::parseObject)
                .keyBy(x -> x.getString("user_id"))
                .process(new KeyedProcessFunction<String, JSONObject, TradePaymentBean>() {
                    ValueState<String> lastPaymentState;

                    @Override
                    public void open(Configuration parameters) {
                        lastPaymentState = getRuntimeContext().getState(
                                new ValueStateDescriptor<>("state", String.class));
                    }

                    @Override
                    public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TradePaymentBean>.Context context, Collector<TradePaymentBean> collector) throws Exception {
                        Long ts = jsonObject.getLong("ts") * 1000;
                        String today = DateFormatUtil.tsToDate(ts);
                        String lastPaymentValue = lastPaymentState.value();
                        Long paymentSucUniqueUserCount = 0L;
                        Long paymentSucNewUserCount = 0L;
                        if (!today.equals(lastPaymentValue)) {
                            paymentSucUniqueUserCount = 1L;
                            if (lastPaymentValue == null) {
                                paymentSucNewUserCount = 1L;
                            }
                            lastPaymentState.update(today);
                        }
                        if (paymentSucUniqueUserCount == 1) {
                            collector.collect(new TradePaymentBean("", "", "",
                                    paymentSucUniqueUserCount, paymentSucNewUserCount, ts));
                        }
                    }
                });
    }
}